import { message } from '@/element-ui/message';
import { nanoid } from 'nanoid';
export default {
    namespaced: true,
    actions: {
        // 用户登录
        login(context, user){
            // 模拟获取正确的用户信息
            let u = { username: 'admin', password: 'admin'}
            if(user.username === u.username && user.password === u.password){
                context.commit('login', user)
            }
            else
                context.commit('logout')
        },
        // 用户注销
        logout(context){
            context.commit('logout')
        },
        // 修改用户信息
        modify(context, [user, callback]){
            if(user.id !== null && user.id !== ''){
                context.commit('modify', [user, callback])
                return true
            }
            else {
                message.fail('修改失败')
            }
        },
        // 删除用户
        remove(context, [userList, callback]){
            if(userList != null && userList.length > 0){
                context.commit('remove', [userList, callback])
                return true
            }
            else {
                message.fail('删除失败')
            }
        },
        // 添加用户
        add(context, [user, callback]){
            if(user != null){
                context.commit('add', [user, callback])
                return true
            } else {
                message.fail('用户信息获取失败, 添加失败')
            }
        },
        // 查找用户
        find(context, [condition, callback]){
            if(condition != null){
                context.commit('find', [condition, callback])
            }
        }
    },
    mutations: {
        login(state, user){
            state.userInfo = user
            state.loginState = true
        },
        logout(state){
            // 清除缓存
            state.userInfo = {}
            state.loginState = false
            window.localStorage.removeItem('userInfo')
        },
        modify(state, [user, callback]){
            let pos = -1
            // 根据ID查找到用户
            for(let i = 0; i < state.userList.length; i++){
                if(state.userList[i].id == user.id){
                    pos = i
                    break
                }
            }
            if(pos != -1){  // 若用户存在，则进行更新
                state.userList.splice(pos, 1, user) 
                // 更新缓存
                localStorage.setItem('userList', JSON.stringify(state.userList))
                message.success('修改成功!')
                if(callback != null)
                    callback()
            }
        },
        remove(state, [userList, callback]){
            let count = 0
            userList.forEach(user => {
                let pos = -1
                // 首先查找用户是否存在
                for(let i = 0; i < state.userList.length; i++){
                    if(state.userList[i].id === user.id){
                        pos = i
                        break
                    }
                }
                if(pos != -1){ // 用户存在, 删除用户
                    state.userList.splice(pos, 1)
                    count++
                }
            })
            // 更新缓存
            localStorage.setItem('userList', JSON.stringify(state.userList))
            message.success('成功删除: ' + count + ' 个用户')
            if(callback != null)
                callback()
        },
        add(state, [user, callback]){
            let id = 0
            // 检查ID是否存在
            let flag = true
            do{
                id = nanoid()
                flag = false
                for(let i = 0; i < state.userList.length; i++){
                    if(state.userList[i].id === id){
                        flag = true
                    }
                }
            } while(flag)

            // 插入用户
            user = {'id': id, ...user}
            state.userList.push(user)
            // 更新缓存
            localStorage.setItem('userList', JSON.stringify(state.userList))
            message.success('添加成功!')
        },
        updatePage(state, pageInfo){
            state.pageInfo = pageInfo
        },
        find(state, [condition, callback]){
            // 初始化数据
            state.userList = state.userListOrigin

            let id = condition.id
            let name = condition.name 
            let userList = state.userList
            if(id !== undefined){
                userList = userList.filter((user) => {
                    return user.id === id
                })
            }
            if(name !== undefined ){
                userList = userList.filter((user) => {
                    return user.name.indexOf(name) != -1
                })
            }
            callback()
            state.userList = userList
        }
    },
    state:{
        // 当前登录用户的信息
        userInfo: 
            JSON.parse(localStorage.getItem('userInfo'))
                || {
                    username: 'admin',
                    password: 'admin',
                    isLogin: false
                },
        loginState: false,
        // 当前用户列表的信息
        userList: JSON.parse(localStorage.getItem('userList')) || [
            {
              id: "1",
              name: "uni",
              password: '123456'
            },
            {
                id: '2',
                name: 'baidu',
                password: '123456'
            },
            {
                id: '3',
                name: 'qowijt',
                password: '123456'
            },
            {
                id: '4',
                name: 'qqp',
                password: '123456'
            },
            {
                id: '5',
                name: 'zpda',
                password: '123456'
            },
            {
                id: '6',
                name: 'zxc',
                password: '123456'
            }
          ],
          userListOrigin: JSON.parse(localStorage.getItem('userList')) || [
            {
              id: "1",
              name: "uni",
              password: '123456'
            },
            {
                id: '2',
                name: 'baidu',
                password: '123456'
            },
            {
                id: '3',
                name: 'qowijt',
                password: '123456'
            },
            {
                id: '4',
                name: 'qqp',
                password: '123456'
            },
            {
                id: '5',
                name: 'zpda',
                password: '123456'
            },
            {
                id: '6',
                name: 'zxc',
                password: '123456'
            }
          ],
    }
}